<!DOCTYPE html>
<html lang="en">

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./node_modules/_vue@2.5.22@vue/dist/vue.js"></script>
    <script src="./lib/vue-resource-1.3.4.js"></script>
    <link rel="stylesheet" href="./lib/bootstrap-3.3.7.css">
</head>

<body>
    <div id="app">



        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加品牌</h3>
            </div>
            <div class="panel-body form-inline">

                <label>
                    Name:
                    <input type="text" v-model="name" class="form-control" @keyup.enter='add'>
                </label>

                <input type="button" value="添加" class="btn btn-primary" @click="add()">
            </div>
        </div>



        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in list" :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.ctime | dateFormat()}}</td>
                    <td>
                        <a href=""  @click.prevent="del(item.id)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>


    </div>
    <script>
        Vue.filter('dateFormat', function (dateStr, pattern = "") {
            var dt = new Date(dateStr);
            //yyyy-mm-dd
            var y = dt.getFullYear();
            var m = dt.getMonth() + 1;
            var d = dt.getDate();
            // return `${y}-${m}-${d}`
            if (pattern.toLowerCase() === 'yyyy-mm-dd') {
                return `${y}-${m}-${d}`
            } else {
                var hh = dt.getHours()
                var mm = dt.getMinutes()
                var ss = dt.getSeconds()

                return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
            }
        });
        // const url = 'http://www.liulongbin.top:3005';
        Vue.http.options.root = 'http://www.liulongbin.top:3005';
        var vm = new Vue({
            el: '#app',
            data: {
                name: '',
                list: [{
                    id: 1,
                    name: '雷憨憨',
                    ctime: new Date()
                }]
            },
            created() {
                //当vm实例的data和methods初始化完后，
                //vm实例会自动执行created 这个生命周期函数
                this.getList()
            },
            methods: {
                getList() {
                    this.$http.get('api/getprodlist').then((res) => {
                        // console.log(res.body)
                        var res = res.body;
                        if (res.status === 0) {
                            this.list = res.message
                        } else {
                            alert('获取数据失败！')
                        }
                    })
                },
                add() {
                    this.$http.post('api/addproduct', {
                        name: this.name
                    }, {
                        emulateJSON: true
                    }).then(res => {
                        console.log(res.body)
                        if (res.body.status === 0) {
                            this.getList();
                            this.name = ''
                        } else {
                            alert('添加数据失败！')
                        }
                    })
                },
                del(id){
                    this.$http.get('api/delproduct/' + id).then(res=>{
                        if (res.body.status === 0) {
                            this.getList();
                        }else{
                            alert('删除数据失败！') 
                        }
                    })
                }
            },

        })
    </script>
</body>

</html>